package com.cyh.testcustomui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

/**
 * 思路：RelativeLayout + 3个View(圆形) + 属性动画反复运动 + 监听动画执行在合适的时间去交换颜色
 * */
public class LoadingCircleView extends RelativeLayout {
    private CircleView mLeftView,mMiddleView,mRightView;
    private int mTranslationDistance = 50;
    private int mAnimationTime = 250;
    public LoadingCircleView(Context context) {
        this(context, null);
    }

    public LoadingCircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //添加三个圆

        mLeftView = getCirecleView(context);
        mLeftView.exchangeColor(Color.BLUE);
        mMiddleView = getCirecleView(context);
        mMiddleView.exchangeColor(Color.GREEN);
        mRightView = getCirecleView(context);
        mRightView.exchangeColor(Color.RED);

        addView(mLeftView);
        addView(mRightView);
        addView(mMiddleView);

        post(new Runnable() {
            @Override
            public void run() {
                //开启动画
                outAnimation();
            }
        });

    }

    public CircleView getCirecleView(Context context) {
        CircleView circleView = new CircleView(context);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MyUtil.dp2px(context, 8), MyUtil.dp2px(context, 8));
        params.addRule(CENTER_IN_PARENT);
        circleView.setLayoutParams(params);
        return circleView;
    }

    //往两边走动画
    private void outAnimation() {
        //左边圆在左边跑动
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView, "translationX", 0, -mTranslationDistance);
        ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView, "translationX", 0, mTranslationDistance);


        AnimatorSet set = new AnimatorSet();
        set.setDuration(mAnimationTime);
        set.playTogether(leftTranslationAnimator, rightTranslationAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                innerAnimator();
            }
        });
        set.start();

    }

    //往里边走动画
    private void innerAnimator() {
        //左边圆在左边跑动
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView, "translationX",  -mTranslationDistance,0);
        ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView, "translationX",  mTranslationDistance, 0);


        AnimatorSet set = new AnimatorSet();
        set.setDuration(mAnimationTime);
        set.playTogether(leftTranslationAnimator, rightTranslationAnimator);
        set.setInterpolator(new AccelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //换颜色
                int temp = mLeftView.getmColor();
                mLeftView.exchangeColor(mMiddleView.getmColor());
                mMiddleView.exchangeColor(mRightView.getmColor());
                mRightView.exchangeColor(temp);
                //往外面跑
                outAnimation();
            }
        });
        set.start();
    }


    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if(visibility == View.GONE || visibility == View.INVISIBLE) {
            mLeftView.clearAnimation();
            mRightView.clearAnimation();
            mMiddleView.clearAnimation();
            removeAllViews();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
